<?php

namespace App\Http\Controllers\Common;

//require base_path().'/vendor/autoload.php';


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Intervention\Image\ImageManagerStatic as Image;

class ImgUploadController extends Controller {

    public function imgUpload(Request $request)
    {
        //图片路径配置
        $logoPath = config('image.logoPath'); //logo或商品列表图保存位置
        $picturePath = config('image.picturePath');//多张图保存位置

        //获取上传信息
        $imageType = $request->post('imageType'); //获取图片类型用来判断是logo或商品列表图，还是多张图
      #  $imagefile = Input::file('image'); //获取上传的图片
$imagefile = $_FILES['image'];
        //定义保存路径和图片前缀
        $path = '';         //图片保存的路径，logo或multiImages
        $prefix = '';       //图片前缀 ，logo或multi
        //确定保存路径
        if($imageType == 1){
            $path = $logoPath;
            $prefix = 'head';
        }else{
            $path = $picturePath;
            $prefix = 'picture';
        }

        $imageName = $this->handlePicture($imagefile,$path,$prefix);
        return json_encode(['imageName'=>$imageName,'status'=>200]);
    }
    public function handlePicture($imagefile=[],$path='/',$prefix='_'){
        $size = config('image.size'); //图片大小
        //处理图片
//        $fileExtension = $imagefile->getClientOriginalExtension(); //获取文件扩展名
        $fileExtension = 'jpg'; //获取文件扩展名
        $imageName = $prefix . md5(uniqid(mt_rand(), true)).'.'.$fileExtension; //生成图片名
        $Img = Image::make($imagefile['tmp_name']);
        if($size['auto']){
            $Img->widen($size['width'],function(){})->save($path.$imageName);
        }else{
            $Img->resize($size['width'],$size['height'])->save($path.$imageName);
        }

        return $imageName;
    }
    
    public function imgUpload2(Request $request){
	return $request;
    }

}
